package edu.hawaii.duedates.tests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

import edu.hawaii.duedates.BookInfoRecord;
import edu.hawaii.duedates.CommandLineArgs;
import edu.hawaii.duedates.DueDates;
import edu.hawaii.duedates.DueDatesMail;
import edu.hawaii.duedates.UniversityOfHawaiiLibrary;

/**
 * The unit test to assure the DueDatesMail is working properly.
 * A check is done on a bad email and smtp server values.
 * A test is done on a valid email and smtp server address.
 * 
 * @author Daniel Arakaki, John Ancheta, Mari-Lee Flestado
 * @version 1.0, November 14, 2008
 *
 */
public class TestDueDatesMail {
  
  /**A String for the uhm option. */
  public static final String UHM_OPTION = "-uhm";
  
  /**A String for the uhm option. */
  public static final String EMAIL_OPTION = "-email";
  
  /** Unexpected error fail string.*/
  public static final String SHOULD_NOT_FAIL = "Should not fail.";
  
  /** An variable for the email address, instantiated to take in a command argument.*/
  private final String emailAddress = System.getenv("EMAIL_ADDRESS");
  
  /** An variable for the SMTP address, instantiated to take in a command argument.*/
  private final String smtpServer = System.getenv("SMTP_SERVER");
  
  /** Instance variable for user id, instantiated to take in command argument. */
  private static final String dueDatesTestUser2Id = 
                              System.getenv("DUEDATES_TESTUSER2_ID");

  /** Instance variable for user last name, instantiated to take in command argument. */
  private static final String dueDatesTestUser2LastName = 
                              System.getenv("DUEDATES_TESTUSER2_LASTNAME");
  
  /** Instance variable for user last name, instantiated to take in command argument. */
  private static final String CONSOLE_OPTION = "-console";
  
  /**
   * Testing a failed delivery.
   * 
   * @throws Exception An exception is thrown when the mail cannot be delivered.
   */
  @Test(expected = Exception.class)
  public void testSendMail() throws Exception {
    DueDatesMail mailer = DueDatesMail.getInstance();
    DueDates dueDate = new DueDates();
    // the strings should be null
    mailer.sendMail(emailAddress, smtpServer, dueDate.getSiteInfo());
  }
  
  /**
   * Testing a properly sent message.
   * 
   * @throws Exception An exception is thrown if there is a problem with parsing the ID and 
   *         last name.
   */
  @Test
  public void testSendMailTwo() throws Exception {
    DueDatesMail mailer = DueDatesMail.getInstance();
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    commandLineArgs.parse(new String[] {CONSOLE_OPTION, UHM_OPTION,
        dueDatesTestUser2Id,
        dueDatesTestUser2LastName});
    DueDates dueDate = new DueDates();
    dueDate.set(commandLineArgs);
    UniversityOfHawaiiLibrary uHm = new UniversityOfHawaiiLibrary();
    List<BookInfoRecord> bookList = new ArrayList<BookInfoRecord>();
    uHm.getUhmInfo(dueDatesTestUser2Id, dueDatesTestUser2LastName, bookList);
    String uhInfo = dueDate.getSiteInfo();
    try {
      assertTrue("Testing for a successfully sent msg.",
          mailer.sendMail("danielma@hawaii.edu", "mail.hawaii.edu", uhInfo));
    }
    catch (Exception e) {
      fail("Should not get here, problems with dueDate.getSiteInfo().");
      e.printStackTrace();
    }
  }
}
